<template>
  <div class="user-leave">
    <div class="query-form">
      <el-form
        ref="lookForm"
        :inline="true"
        :model="queryForm"
        style="display: flex"
      >
        <div class="query" style="display: flex">
          <el-form-item label="审批状态" prop="applyState">
            <el-select v-model="queryForm.applyState">
              <el-option value="" label="全部"></el-option>
              <el-option value="1" label="待审批"></el-option>
              <el-option value="2" label="审批中"></el-option>
              <el-option value="3" label="审批拒绝"></el-option>
              <el-option value="4" label="审批通过"></el-option>
              <el-option value="5" label="作废"></el-option>
            </el-select>
          </el-form-item>
        </div>
        <div class="clickQuery" style="margin-left: 30px; width: 200px">
          <el-button type="primary" @click="onSubmit()">查询</el-button>
          <el-button @click="reset">重置</el-button>
        </div>
      </el-form>
    </div>

    <div class="base-table">
      <el-table
        ref="multipleTableRef"
        :data="leaveList"
        style="width: 100%"
        row-key="_id"
      >
        <el-table-column
          v-for="item in columns"
          :key="item.prop"
          :prop="item.prop"
          :label="item.label"
          :formatter="item.formatter"
        ></el-table-column>

        <el-table-column label="操作" width="250">
          <template #default="scope">
            <el-button type="primary" @click="approveList(scope.row)"
              >审批</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-dialog v-model="showMadol" title="审核">
      <el-form ref="dialogForm" :model="approveForm">
        <el-form-item label="申请人:" required>{{
          approveForm.applyUser.userName
        }}</el-form-item>
        <el-form-item label="休假类型:" required
          >{{ approveForm.applyType }}
        </el-form-item>
        <el-form-item label="休假时间:" required>{{
          approveForm.date
        }}</el-form-item>
        <el-form-item label="休假时长:">{{
          approveForm.leaveTime
        }}</el-form-item>
        <el-form-item label="休假原因:">{{ approveForm.reasons }}</el-form-item>
        <el-form-item label="审批状态:">{{
          approveForm.applyState
        }}</el-form-item>
        <el-form-item label="审批人:">{{
          approveForm.curAuditUserName
        }}</el-form-item>
        <el-form-item label="备注:" prop="remarks">
          <el-input
            v-model="approveForm.remarks"
            :rows="2"
            type="textarea"
            placeholder="请输入审核备注"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="go">审核通过</el-button>
          <el-button @click="back">驳回</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
  <el-pagination background layout="prev, pager, next" :total="1000" />
</template>
<!-- pass refuse -->
<script setup>
import api from "../api/index";
import { ref, reactive, toRaw, nextTick, computed } from "vue";
import utils from "../utils/utils";

// 定义查询状态列表
const queryForm = reactive({});
// 定义查询按钮
const onSubmit = async () => {
  console.log(queryForm);
  let res = await api.getLeaveList(queryForm);
  console.log(res.list);
  leaveList.value = res.list;
};
// 定义申请请假列表
const leaveForm = ref({});
// 定义请假列表
let leaveList = ref([]);
// 定义动态表格格式
const columns = reactive([
  {
    label: "单号",
    prop: "orderNo",
  },
  {
    label: "申请人",
    prop: "applyUser.userName",
  },
  {
    label: "休假时间",
    prop: "startTime",
    formatter: (row, column, value) => {
      return `${row.startTime.slice(0, 10)}-${row.endTime.slice(0, 10)}`;
    },
  },
  {
    label: "休假时长",
    prop: "leaveTime",
  },
  {
    label: "休假类型",
    prop: "applyType",
    formatter: (row, column, value) => {
      return {
        1: "事假",
        2: "调休",
        3: "年假",
      }[value];
    },
  },
  {
    label: "休假原因",
    prop: "reasons",
  },
  {
    label: "申请时间",
    prop: "createTime",
    formatter: (row, column, value) => {
      return utils.formateDate(new Date(value));
    },
  },
  {
    label: "审批人",
    prop: "auditUsers",
  },
  {
    label: "当前审批人",
    prop: "curAuditUserName",
  },
  {
    label: "审批状态",
    prop: "applyState",
    formatter: (row, column, value) => {
      return {
        1: "待审批",
        2: "审批中",
        3: "审批拒绝",
        4: "审批通过",
        5: "作废",
      }[value];
    },
  },
]);
// 获取初始化的请假列表
let data = {
  type: "",
};
const getApproveList = async () => {
  let res = await api.getLeaveList(data);
  console.log(res.list);
  console.log(res.list.endTime);
  res.list.forEach((ele) => {
    if (!ele.leaveTime) {
      let eT = +new Date(ele.endTime);
      let sT = +new Date(ele.startTime);
      ele["leaveTime"] = (eT - sT) / 86400000 + "天";
      console.log(ele["leaveTime"]);
    }
  });
  leaveList.value = res.list;
};
getApproveList();
// 定义获取表单节点dialogForm
const dialogForm = ref(null);
// 定义记录用户行为
const action = ref("pass");
// 定义弹窗showMadol
const showMadol = ref(false);
// 定义表单重置方法
const resetForm = () => {
  dialogForm.value.resetFields();
};
// 定义审批弹窗每条数据
const approveForm = ref({});
// 定义审批按钮
const approveList = (row) => {
  showMadol.value = true;
  console.log(row);
  approveForm.value = row;
  approveForm.value.date = `${row.startTime.slice(0, 10)}-${row.endTime.slice(
    0,
    10
  )}`;
};
// 定义审批通过按钮;
const go = () => {
  dialogForm.value.validate(async (vaild) => {
    if (vaild) {
      action.value = "pass";
      const alldata = toRaw(approveForm);
      const data = {
        _id: alldata.value._id,
        remarks: alldata.value.remarks,
        action: action.value,
      };
      let res = await api.operateLeave(data);
      console.log(res);
      resetForm();
      showMadol.value = false;
      getApproveList();
    }
  });
};
// 定义审批驳回按钮;
const back = () => {
  dialogForm.value.validate(async (vaild) => {
    if (vaild) {
      action.value = "refuse";
      const alldata = toRaw(approveForm);
      const data = {
        _id: alldata.value._id,
        remarks: alldata.value.remarks,
        action: action.value,
      };
      let res = await api.operateLeave(data);
      console.log(res);
      resetForm();
      showMadol.value = false;
      getApproveList();
    }
  });
};
</script>

<style lang="scss" scoped></style>
